<template>
  <div class="h-full">
    <a-card class="w-full h-full" :body-style="{ height: '100%', overflow: 'auto' }">
      <div class="flex justify-between h-full">
        <div class="w-2/3 h-full flex flex-col">
          <BaseTitle title="选择数据" />
          <a-divider class="my-1" />
          <div class="overflow-y-scroll mb-[40px]">
            <div v-for="(item, index) in itemConfig" :key="index">
              <div v-if="showConfig[item.label]" class="left-row flex justify-between h-10 mb-1">
                <div class="flex-1 flex items-center pl-2">{{ item.label }}</div>
                <div
                  :class="item.active === 1 ? 'bg-primary text-white' : ''"
                  class="w-5/12 flex items-center cursor-pointer left-border"
                  @click="checkSelect(item, 1, index)"
                >
                  {{ patientOne[item.value] }}
                </div>
                <div
                  :class="item.active === 2 ? 'bg-primary text-white' : ''"
                  class="w-5/12 flex items-center cursor-pointer left-border"
                  @click="checkSelect(item, 2, index)"
                >
                  {{ patientTwo[item.value] }}
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="flex-1 ml-4 flex flex-col">
          <BaseTitle title="合并后数据" />
          <a-divider class="my-1" />
          <div class="overflow-y-scroll flex-1">
            <div v-for="(item, index) in itemConfig" :key="index">
              <div v-if="showConfig[item.label]" class="left-row flex justify-between h-10 mb-1">
                <div class="w-1/3 flex items-center pl-2">{{ item.label }}</div>
                <div class="w-2/3 flex items-center cursor-pointer left-border">
                  {{ resultInfo[item.value] }}
                </div>
              </div>
            </div>
          </div>
          <div class="flex justify-end mt-2">
            <a-button @click="router.go(-1)">返回</a-button>
            <a-button class="ml-2" @click="reset">重置</a-button>
            <a-button type="primary" class="ml-2" @click="saveInfo">确定</a-button>
          </div>
        </div>
      </div>
    </a-card>
  </div>
</template>
<script setup>
import { message } from 'ant-design-vue'

const router = useRouter()

const route = useRoute()
const itemConfig = ref([
  { label: '卡号', value: 'brId', active: 0 },
  { label: '姓名', value: 'brName', active: 0 },
  { label: '性别', value: 'brXb', active: 0 },
  // { label: '病历号', value: 'brBlkh', active: 0 },
  { label: '证件类型', value: 'brZjlx', active: 0 },
  { label: '证件号码', value: 'brSfz', active: 0 },
  { label: '手术病案号', value: 'oprSn', active: 0 },
  { label: '出生日期', value: 'brBtd', active: 0 },
  { label: '年龄', value: 'brAge', active: 0 },
  { label: '手机号', value: 'brMob', active: 0 },
  { label: '备用手机号', value: 'backupPhone', active: 0 },
  { label: '婚姻状况', value: 'brHf', active: 0 },
  { label: '电话', value: 'brJtdh', active: 0 },
  { label: '邮件', value: 'brEmail', active: 0 },
  { label: '家庭地址', value: 'brJtdz', active: 0 },
  { label: '户籍地址', value: 'brHkdz', active: 0 },
  { label: '邮编', value: 'brJtpostno', active: 0 },
  { label: '民族', value: 'brMz', active: 0 },
  { label: '国籍', value: 'brGj', active: 0 },
  { label: '户籍', value: 'brJg', active: 0 },
  { label: '身高', value: 'brSg', active: 0 },
  { label: '体重', value: 'brTz', active: 0 },
  { label: '文化程度', value: 'brWhcd', active: 0 },
  { label: '血型', value: 'brXx', active: 0 },
  { label: 'RH', value: 'rhxx', active: 0 },
  { label: '人员属性', value: 'rysx', active: 0 },
  { label: '联系人', value: 'brLxr', active: 0 },
  { label: '联系人关系', value: 'brLxrgx', active: 0 },
  { label: '联系人电话', value: 'brLxrdh', active: 0 },
  { label: '职业', value: 'brZy', active: 0 },
  { label: '工作单位', value: 'brGzdw', active: 0 },
  { label: '工作地址', value: 'brGzdz', active: 0 },
  { label: '政治面貌', value: 'brZzmm', active: 0 },
  { label: '入伍日期', value: 'brRwny', active: 0 },
  { label: '部队番号', value: 'brBdfh', active: 0 },
  { label: '部别', value: 'brBb', active: 0 },
  { label: '军种', value: 'brJz', active: 0 },
  { label: '军衔', value: 'brJx', active: 0 },
  // { label: '(年龄不足1周岁)年龄', value: 'brBzAge',  active: 0  },
  { label: '学校', value: 'school', active: 0 },
  { label: '年级', value: 'grade', active: 0 },
  { label: '班级', value: 'bj', active: 0 },
  { label: '新生儿出生体重', value: 'brXsecstz', active: 0 },
  { label: '新生儿入院体重', value: 'brXserytz', active: 0 },
  { label: '既往史', value: 'brJws', active: 0 },
  { label: '过敏史', value: 'brGms', active: 0 },
  { label: '遗传史', value: 'brYcs', active: 0 },
  { label: '家族史', value: 'brJzs', active: 0 },
  { label: '月经及生育史', value: 'brYjsjsys', active: 0 },
  { label: '暴露史', value: 'brBls', active: 0 },
  { label: '残疾情况', value: 'brCjqk', active: 0 },
  { label: '职业病', value: 'brZyb', active: 0 }
])
const patientOne = ref({})
const patientTwo = ref({})
const resultInfo = ref({})
const getPatientInfo = async () => {
  const { code, msg, data } = await post('/outp/brDataChange/getPatientListByBrIds', {
    brIds: route.query.ids
  })
  if (code !== 0) return message.error(msg)
  if (data && data.length === 2) {
    patientOne.value = data[0]
    patientTwo.value = data[1]
    getShowConfig()
  } else {
    message.error(msg || '获取信息失败')
  }
}
const saveInfo = async () => {
  const { msg, code } = await post('/outp/brDataChange/mergePatient', {
    originBrId: itemConfig.value[0].active === 1 ? patientTwo.value.brId : patientOne.value.brId,
    destBrId: resultInfo.value.brId,
    moduleBrxxbEntity: resultInfo.value
  })
  if (code !== 0) return message.error(msg || '保存失败')
  message.success(msg || '保存成功')
  router.go(-1)
}
const showConfig = ref({})
const getShowConfig = async () => {
  const { data, msg, code } = await post('/operation/opex/getPatientlnfoList', {
    managementType: '0'
  })
  if (code !== 0) return message.error(msg || '获取数据失败')
  const childrenArray = data.map((item) => item.children).flat()
  childrenArray.forEach((child) => {
    showConfig.value[child.patientInfoName] = child.status === 0
  })
  showConfig.value['卡号'] = true
  itemConfig.value.forEach((item) => {
    if (showConfig.value[item.label]) {
      if ((patientOne.value[item.value] || patientOne.value[item.value] === 0) && (patientTwo.value[item.value] || patientTwo.value[item.value] === 0)) {
        item.active = 1
        resultInfo.value[item.value] = patientOne.value[item.value]
      } else if (patientOne.value[item.value] || patientOne.value[item.value] === 0) {
        item.active = 1
        resultInfo.value[item.value] = patientOne.value[item.value]
      } else if (patientTwo.value[item.value] || patientTwo.value[item.value] === 0) {
        item.active = 2
        resultInfo.value[item.value] = patientTwo.value[item.value]
      } else {
        item.active = 0
        resultInfo.value[item.value] = null
      }
    }
  })
}
const checkSelect = (item, val, idx) => {
  if (val === 1) {
    if (patientOne.value[item.value] || patientOne.value[item.value] === 0) {
      itemConfig.value[idx].active = 1
      resultInfo.value[item.value] = patientOne.value[item.value]
    }
  } else {
    if (patientTwo.value[item.value] || patientTwo.value[item.value] === 0) {
      itemConfig.value[idx].active = 2
      resultInfo.value[item.value] = patientTwo.value[item.value]
    }
  }
}
const reset = () => {
  itemConfig.value.forEach((item) => {
    if (showConfig.value[item.label]) {
      if ((patientOne.value[item.value] || patientOne.value[item.value] === 0) && (patientTwo.value[item.value] || patientTwo.value[item.value] === 0)) {
        item.active = 1
        resultInfo.value[item.value] = patientOne.value[item.value]
      } else if (patientOne.value[item.value] || patientOne.value[item.value] === 0) {
        item.active = 1
        resultInfo.value[item.value] = patientOne.value[item.value]
      } else if (patientTwo.value[item.value] || patientTwo.value[item.value] === 0) {
        item.active = 2
        resultInfo.value[item.value] = patientTwo.value[item.value]
      } else {
        item.active = 0
        resultInfo.value[item.value] = null
      }
    }
  })
}

onMounted(() => {
  if (route.query.ids.length === 2) {
    getPatientInfo()
  }
})
</script>
<style lang="less" scoped>
.left-row {
  background-color: #f8f8f8;
  border: 1px solid #f3f3f3;
}
.left-border {
  border-left: 1px solid #f3f3f3;
  padding-left: 8px;
}
</style>
